package com.example.administrator.energyida.login;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.energyida.R;
import com.example.administrator.energyida.util.OceanUtil;
import com.example.administrator.energyida.util.Validator;
import com.jaeger.library.StatusBarUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends Activity implements View.OnClickListener {

    private TextView text_register_back;
    private TextView text_register_title;
    private RelativeLayout relativeLayout_register;
    private ImageView img_register_logo;
    private EditText edit_register_email;
    private EditText edit_register_password;
    private EditText edit_register_confirm;
    private Button btn_register_signUp;

    private Handler handler;

    private String response1;
    private String response2;
    private String email;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();

        initHandler();

        setListeners();
    }

    @SuppressLint("HandlerLeak")
    private void initHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                SharedPreferences spReg = getSharedPreferences("reg", MODE_PRIVATE);
                SharedPreferences.Editor editorReg = spReg.edit();
                editorReg.clear().commit();

                switch (msg.what) {
                    case 1:
                        if (response1.contains("10008")) {
                            Toast.makeText(RegisterActivity.this, "邮箱可用", Toast.LENGTH_SHORT).show();
                        } else if (response1.contains("20008")) {
                            Toast.makeText(RegisterActivity.this, "邮箱已存在", Toast.LENGTH_SHORT).show();
                            edit_register_email.setText("");
                            edit_register_email.requestFocus();
                        }
                        break;
                    case 2:
                        if (response2.contains("10009")) {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            intent.putExtra("email", email);
                            intent.putExtra("password", password);
                            startActivity(intent);
                        }
                        if (response2.contains("20009")) {
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            edit_register_email.setText("");
                            edit_register_email.requestFocus();
                        }
                        break;
                    default:
                        break;
                }
            }
        };
    }

    private void initView() {
        text_register_back = findViewById(R.id.text_register_back);
        text_register_title = findViewById(R.id.text_register_title);
        relativeLayout_register = findViewById(R.id.relativeLayout_register);
        img_register_logo = findViewById(R.id.img_register_logo);
        edit_register_email = findViewById(R.id.edit_register_email);
        edit_register_password = findViewById(R.id.edit_register_password);
        edit_register_confirm = findViewById(R.id.edit_register_confirm);
        btn_register_signUp = findViewById(R.id.btn_register_signUp);

        btn_register_signUp.setOnClickListener(this);
        text_register_back.setOnClickListener(this);

        // 设置状态栏的颜色
        StatusBarUtil.setColor(this,Color.parseColor("#10cafd"), 50);
    }

    private void setListeners() {
        edit_register_email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    submit();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register_signUp:
                signUp();
                break;
            case R.id.text_register_back:
                finish();
                break;
            default:
                break;
        }
    }

    private void submit() {
        // validate
        email = edit_register_email.getText().toString().trim();
        if (!Validator.isEmail(email)) {
            Toast.makeText(this, "请输入正确的邮箱格式", Toast.LENGTH_SHORT).show();
            edit_register_email.setText("");
        } else {
            JSONObject jsonObject = new JSONObject();

            try {
                jsonObject.put("email", email);
                jsonObject.put("mode", 1);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            new OceanUtil("register.php", jsonObject, new OceanUtil.HttpRequestCallback() {
                @Override
                public void callback(String strResponse) {
                    response1 = strResponse;
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                }
            }).postInfo();
        }

    }

    private void signUp() {
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(this, "请输入邮箱", Toast.LENGTH_SHORT).show();
            return;
        }

        password = edit_register_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!Validator.isPassword(password)) {
            Toast.makeText(this, "请输入3-16位的密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String confirm = edit_register_confirm.getText().toString().trim();
        if (TextUtils.isEmpty(confirm)) {
            Toast.makeText(this, "请确认密码", Toast.LENGTH_SHORT).show();
        } else {
            if (password.equals(confirm)) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("email", email);
                    jsonObject.put("password", password);
                    jsonObject.put("mode", 2);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                new OceanUtil("register.php", jsonObject, new OceanUtil.HttpRequestCallback() {
                    @Override
                    public void callback(String strResponse) {
                        response2 = strResponse;
                        Message message = new Message();
                        message.what = 2;
                        handler.sendMessage(message);
                    }
                }).postInfo();
            } else {
                edit_register_confirm.setText("");
                edit_register_confirm.setFocusable(true);
                edit_register_confirm.setFocusableInTouchMode(true);
            }
        }

    }
}
